Skip to content

Fix adjoint sensitivities for events that are both state-triggered and state-updating - #3258

Open
dweindl wants to merge 5 commits into
AMICI-dev:mainfrom
dweindl:fix-asa-event-trigger-time-sensitivity
Open

Fix adjoint sensitivities for events that are both state-triggered and state-updating#3258
dweindl wants to merge 5 commits into
AMICI-dev:mainfrom
dweindl:fix-asa-event-trigger-time-sensitivity

Conversation

@dweindl

@dweindl dweindl commented Sep 8, 2026

Copy link
Copy Markdown
Member

deltaxB/deltaqB embed dtaudx/dtaudp (trigger-time-sensitivity terms), but these were evaluated at whatever state the generated function happens to be called with -- in practice the post-event state. That's the wrong side of the discontinuity: the equivalent forward-mode quantity (stau) is deliberately computed pre-event. Fix: eliminate w via its own algebraic definition, then substitute x -> x_old.

Verified via direct FSA-vs-ASA comparison (bypassing FD entirely) on a 72-case corpus of affected SBML semantic test suite models: divergence for the affected parameter previously ranged from ~0.001% up to ~1% relative (worst observed: FSA=0.16456 vs ASA=0.16303); after the fix, it drops to ~1e-13-3e-11 relative (floating-point noise) with zero regressions.

Corpus (SBML semantic test suite case IDs), for future reference/test cases:

  • Explicit SBML events (state-triggered and state-updating): 00026, 00041, 00074, 00619, 00620, 00621, 00634, 00635, 00636, 00646, 00647, 00648, 00652, 00653, 00654, 00679, 00680, 00681, 00689, 00700, 00707, 00723, 00736, 00743, 00744, 00745, 00746, 00747, 00749, 00750, 00751, 00752, 00753, 00754, 00755, 00756, 00789, 00790, 00791, 00845, 00846, 00847, 00883, 00884, 00945, 00946, 00947, 00948, 01045, 01046, 01047, 01071, 01072, 01073, 01094, 01106
  • Piecewise reaction-rate laws (no explicit , but AMICI hits the same code path via an internal root): 00191, 00192, 00193, 00194, 00198, 00199, 00200, 00201, 00270, 00272, 00273, 00274, 00276, 00277, 00278, 00279

Co-Authored-By: Claude Sonnet 5 noreply@anthropic.com

Fixes #3257. Still some way to go to fix #18.
Tests are implemented as part of #3251 and #3253.

@codecov

codecov Bot commented Sep 8, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 22.22222% with 21 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.95%. Comparing base (8c742fd) to head (1aee264).

Files with missing lines Patch % Lines
models/model_events_py/deltaxB.cpp 0.00% 7 Missing ⚠️
models/model_nested_events_py/deltaxB.cpp 0.00% 3 Missing ⚠️
models/model_neuron_py/deltaxB.cpp 0.00% 3 Missing ⚠️
models/model_calvetti_py/model_calvetti_py.h 0.00% 1 Missing ⚠️
models/model_dirac_py/model_dirac_py.h 0.00% 1 Missing ⚠️
models/model_events_py/model_events_py.h 0.00% 1 Missing ⚠️
...odel_jakstat_adjoint_py/model_jakstat_adjoint_py.h 0.00% 1 Missing ⚠️
...ls/model_nested_events_py/model_nested_events_py.h 0.00% 1 Missing ⚠️
models/model_neuron_py/model_neuron_py.h 0.00% 1 Missing ⚠️
models/model_robertson_py/model_robertson_py.h 0.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3258      +/-   ##
==========================================
- Coverage   78.52%   77.95%   -0.57%     
==========================================
  Files         318      318              
  Lines       22197    22203       +6     
  Branches     1491     1491              
==========================================
- Hits        17430    17309     -121     
- Misses       4759     4886     +127     
  Partials        8        8              
Flag Coverage Δ
cpp 72.30% <22.22%> (+<0.01%) ⬆️
cpp_python 35.26% <22.22%> (+0.02%) ⬆️
petab 48.45% <100.00%> (+0.02%) ⬆️
petab_sciml 16.45% <16.66%> (+<0.01%) ⬆️
petab_sciml_benchmarks 15.00% <16.66%> (+<0.01%) ⬆️
python 70.75% <22.22%> (+<0.01%) ⬆️
sbmlsuite-jax ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
python/sdist/amici/_symbolic/de_model.py 91.85% <100.00%> (+0.05%) ⬆️
models/model_calvetti_py/model_calvetti_py.h 55.55% <0.00%> (ø)
models/model_dirac_py/model_dirac_py.h 55.86% <0.00%> (ø)
models/model_events_py/model_events_py.h 64.02% <0.00%> (ø)
...odel_jakstat_adjoint_py/model_jakstat_adjoint_py.h 68.45% <0.00%> (ø)
...ls/model_nested_events_py/model_nested_events_py.h 60.66% <0.00%> (ø)
models/model_neuron_py/model_neuron_py.h 70.44% <0.00%> (ø)
models/model_robertson_py/model_robertson_py.h 50.71% <0.00%> (ø)
models/model_steadystate_py/model_steadystate_py.h 60.71% <0.00%> (ø)
models/model_nested_events_py/deltaxB.cpp 0.00% <0.00%> (ø)
... and 2 more

... and 3 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dweindl dweindl self-assigned this Sep 8, 2026
@dweindl
dweindl force-pushed the fix-asa-event-trigger-time-sensitivity branch 2 times, most recently from 583959f to 8a7efac Compare September 9, 2026 20:28
@dweindl dweindl added the events related to handing discrete events label Sep 10, 2026
@dweindl
dweindl force-pushed the fix-asa-event-trigger-time-sensitivity branch from 8a7efac to 67589de Compare September 10, 2026 07:23
"01104": True,
"01107": True,
"01148": True,
# Adjoint sensitivities & events. To revisit later.

@dweindl dweindl Sep 10, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check if we can unskip anything in #3263

@dweindl
dweindl marked this pull request as ready for review September 10, 2026 11:39
@dweindl
dweindl requested a review from a team as a code owner September 10, 2026 11:39
@dweindl
dweindl added this pull request to the merge queue Sep 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Sep 10, 2026
dweindl and others added 4 commits September 10, 2026 18:13
…d state-updating

deltaxB/deltaqB embed dtaudx/dtaudp (trigger-time-sensitivity terms), but
these were evaluated at whatever state the generated function happens to
be called with -- in practice the post-event state. That's the wrong side
of the discontinuity: the equivalent forward-mode quantity (stau) is
deliberately computed pre-event. Fix: eliminate w via its own algebraic
definition, then substitute x -> x_old.

Verified via direct FSA-vs-ASA comparison (bypassing FD entirely) on a
72-case corpus of affected SBML semantic test suite models: divergence
for the affected parameter previously ranged from ~0.001% up to ~1%
relative (worst observed: FSA=0.16456 vs ASA=0.16303); after the fix, it
drops to ~1e-13-3e-11 relative (floating-point noise) with zero
regressions.

Corpus (SBML semantic test suite case IDs), for future reference/test
cases:

- Explicit SBML events (state-triggered and state-updating): 00026,
  00041, 00074, 00619, 00620, 00621, 00634, 00635, 00636, 00646, 00647,
  00648, 00652, 00653, 00654, 00679, 00680, 00681, 00689, 00700, 00707,
  00723, 00736, 00743, 00744, 00745, 00746, 00747, 00749, 00750, 00751,
  00752, 00753, 00754, 00755, 00756, 00789, 00790, 00791, 00845, 00846,
  00847, 00883, 00884, 00945, 00946, 00947, 00948, 01045, 01046, 01047,
  01071, 01072, 01073, 01094, 01106
- Piecewise reaction-rate laws (no explicit <event>, but AMICI hits the
  same code path via an internal root): 00191, 00192, 00193, 00194,
  00198, 00199, 00200, 00201, 00270, 00272, 00273, 00274, 00276, 00277,
  00278, 00279

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dweindl
dweindl added this pull request to the merge queue Sep 10, 2026
@dweindl
dweindl removed this pull request from the merge queue due to a manual request Sep 10, 2026
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@dweindl
dweindl force-pushed the fix-asa-event-trigger-time-sensitivity branch from 33a26aa to 1aee264 Compare September 10, 2026 16:28
@dweindl
dweindl enabled auto-merge September 10, 2026 16:29
@dweindl
dweindl added this pull request to the merge queue Sep 10, 2026
@dweindl
dweindl removed this pull request from the merge queue due to a manual request Sep 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

events related to handing discrete events

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adjoint sensitivities wrong for events that are both state-triggered and state-updating adjoint sensitivies for models with events

2 participants